import 'package:bruno/bruno.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:pin_code_fields/pin_code_fields.dart';



import '../../AppConfig/Config.dart';
import '../../AppConfig/RouteAct.dart';
import '../../http/api.dart';
import '../../l10n/messages.dart';
import 'PayAuthentication.dart';
import 'VerifyBankCardIdentity.dart';

class BankCardDetails extends StatefulWidget {
  const BankCardDetails({super.key, required this.dataMap});
  final Map dataMap;
  @override
  State<BankCardDetails> createState() => _BankCardDetailsState();
}

class _BankCardDetailsState extends State<BankCardDetails> {
  @override
  Widget build(BuildContext context) {
    var messages = AppLocalizations.of(context)!;
    final screenSize = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: MyColors.backgreyColor,
      appBar: AppBar(

        backgroundColor: Colors.white,
        centerTitle: true,
        leading: IconButton(icon: Icon(Icons.arrow_back_ios,color: Colors.black,),onPressed: (){Navigator.pop(context);},),
        title: Text('银行卡详情',style: TextStyle(color: Colors.black,fontWeight: FontWeight.w300,fontSize: 16.sp),),
      ),
      body: ListView(
        children: [
          Container(
            margin: EdgeInsets.only(left: 10.w,right: 10.w,top: 8.h),
            padding: EdgeInsets.only(top:14.h,bottom: 10.h ,left: 13.w),
            decoration: BoxDecoration(
              color:Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(10)),
            ),
            child: Column(
              children: [
                Row(
                  children: [
                    Image.asset("lib/Images/PersonCenter/银行卡icon.png",width: 20.w,height: 20.h,),
                    SizedBox(width: 4.w,),
                    Text(widget.dataMap['bankName'],style: TextStyle(color: Colors.black,fontSize: 16.sp),),
                  ],
                ),
                Row(
                  children: [
                    SizedBox(width: 30.w,),
                    Text(widget.dataMap['type'],style: TextStyle(color: MyColors.textColor,fontSize: 14.sp),),
                  ],
                ),
                Text("····"+" "+"····"+" "+"····"+" "+widget.dataMap['bankCard'].substring(widget.dataMap['bankCard'].length - 4),style: TextStyle(color: Colors.black,fontSize: 26.sp,fontWeight: FontWeight.w600),),
              ],
            ),
          ),
          SizedBox(height: 8.h,),
          GestureDetector(
            child:  Container(
              margin: EdgeInsets.only(left: 10.w,right: 10.w),
              padding: EdgeInsets.only(top: 10.h,bottom: 10.h,left: 10.w,right: 10.w),
              decoration: BoxDecoration(
                color:Colors.white,
                borderRadius: BorderRadius.all(Radius.circular(10)),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Row(
                    children: [
                      Image.asset("lib/Images/PersonCenter/解绑银行卡icon.png",width: 24.w,height: 24.h,),
                      SizedBox(width: 4.w,),
                      Text("解绑银行卡",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                    ],
                  ),
                  Icon(Icons.arrow_forward_ios,color: MyColors.hintColor,size: 18,),
                ],
              ),
            ),
            onTap: (){
              BrnDialogManager.showConfirmDialog(context,
                message:"解绑银行卡将不能再使用，确定解绑尾号为"+widget.dataMap['bankCard'].substring(widget.dataMap['bankCard'].length - 4)+"的银行卡吗？",
                // barrierDismissible:false,
                cancel: '',
                cancelWidget: GestureDetector(
                  child: Center(
                    child:  Text("我再想想",style: TextStyle(color: Colors.black,fontSize: 14.sp),),
                  ),
                  onTap: (){
                    Navigator.pop(context);
                  },
                ),
                conformWidget:GestureDetector(
                  child:  Center(
                    child:  Text("确定解绑",style: TextStyle(color: MyColors.themeColor,fontSize: 14.sp),),
                  ),
                  onTap: (){

                    Navigator.pop(context);
                    // Navigator.push( context, FadeRoute(page:VerifyBankCardIdentity()));
                    showDialog(
                      context: context,
                      builder: (BuildContext context) {
                        return EnterPasswordDialog(id:widget.dataMap['id'].toString(),

                        );
                      },
                    );
                  },
                ),
                confirm: '', );
            },
          ),
        ],
      ),
    );
  }
}
class EnterPasswordDialog extends StatefulWidget {
  const EnterPasswordDialog({super.key, required this.id,});
  final String id;
  @override
  State<EnterPasswordDialog> createState() => _EnterPasswordDialogState();
}

class _EnterPasswordDialogState extends State<EnterPasswordDialog> {
  TextEditingController _controller = TextEditingController();
  @override

  Widget build(BuildContext context) {
    return Dialog(
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(16),
      ),
      child: Container(

        padding: EdgeInsets.all(16),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(10)),
          color: Colors.white,
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                SizedBox(),
                IconButton(
                  icon: Icon(Icons.close,color: MyColors.textColor,),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
              ],
            ),

            Text('请输入支付密码', style: TextStyle(color: Colors.black, fontSize: 16.sp, fontWeight: FontWeight.bold,),),
            SizedBox(height: 16),
            Container(
              margin: EdgeInsets.only(left: 20.w,right:20.w),
              child: PinCodeTextField(
                  obscureText:true,
                  appContext: context,//context
                  length: 6,//输入的长度
                  controller: _controller,
                  autoFocus: true,
                  textStyle:TextStyle(color: Colors.black),
                  cursorColor: Colors.black,
                  pinTheme: PinTheme(
                    shape: PinCodeFieldShape.box, //输入框的形状
                    borderRadius: const BorderRadius.all(Radius.circular(0)),
                    activeColor:MyColors.textColor,
                    selectedColor:MyColors.textColor,
                    inactiveColor:MyColors.backgreyColor,
                  ),
                  onChanged: (value)  async {
                    if(value.length==6) {
                      ApiManager.postData(
                        context,
                        "api/pay/pay/shipper/bankcard/delete",
                        {
                          "id":widget.id,
                          "password":value,
                        },
                            (responseData)  {
                          Navigator.pop(context);
                          Navigator.pop(context,"success");
                        },
                      );
                    }
                  }
              ),
            ),

            SizedBox(height: 16),
            TextButton(
              onPressed: () {
                Navigator.pop(context);
                Navigator.push( context, FadeRoute(page:PayAuthentication(phoneNum: "",)));

              },
              child: Text('忘记密码？',
                style: TextStyle(
                  color: MyColors.themeColor,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}